//
//    RootClass.swift
//
//    Create by 徐挺 陈 on 13/5/2018
//    Copyright © 2018. All rights reserved.
//    Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation
import SwiftyJSON
import PromiseKit


class DebitStyle : NSObject, NSCoding{
    
    var actualMoney : Float!
    var dailyInterest : Float!
    var debitFee : Float!
    var debitMoney : Float!
    var debitPeriod : Int!
    var descriptionField : String!
    var overdueDayInterest : Float!
    var payBackMoney : Float!
    
    
    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON!){
        if json.isEmpty{
            return
        }
        actualMoney = json["actualMoney"].floatValue
        dailyInterest = json["dailyInterest"].floatValue
        debitFee = json["debitFee"].floatValue
        debitMoney = json["debitMoney"].floatValue
        debitPeriod = json["debitPeriod"].intValue
        descriptionField = json["description"].stringValue
        overdueDayInterest = json["overdueDayInterest"].floatValue
        payBackMoney = json["payBackMoney"].floatValue
    }
    
    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if actualMoney != nil{
            dictionary["actualMoney"] = actualMoney
        }
        if dailyInterest != nil{
            dictionary["dailyInterest"] = dailyInterest
        }
        if debitFee != nil{
            dictionary["debitFee"] = debitFee
        }
        if debitMoney != nil{
            dictionary["debitMoney"] = debitMoney
        }
        if debitPeriod != nil{
            dictionary["debitPeriod"] = debitPeriod
        }
        if descriptionField != nil{
            dictionary["description"] = descriptionField
        }
        if overdueDayInterest != nil{
            dictionary["overdueDayInterest"] = overdueDayInterest
        }
        if payBackMoney != nil{
            dictionary["payBackMoney"] = payBackMoney
        }
        return dictionary
    }
    
    /**
     * NSCoding required initializer.
     * Fills the data from the passed decoder
     */
    @objc required init(coder aDecoder: NSCoder)
    {
        actualMoney = aDecoder.decodeObject(forKey: "actualMoney") as? Float
        dailyInterest = aDecoder.decodeObject(forKey: "dailyInterest") as? Float
        debitFee = aDecoder.decodeObject(forKey: "debitFee") as? Float
        debitMoney = aDecoder.decodeObject(forKey: "debitMoney") as? Float
        debitPeriod = aDecoder.decodeObject(forKey: "debitPeriod") as? Int
        descriptionField = aDecoder.decodeObject(forKey: "description") as? String
        overdueDayInterest = aDecoder.decodeObject(forKey: "overdueDayInterest") as? Float
        payBackMoney = aDecoder.decodeObject(forKey: "payBackMoney") as? Float
        
    }
    
    /**
     * NSCoding required method.
     * Encodes mode properties into the decoder
     */
    func encode(with aCoder: NSCoder)
    {
        if actualMoney != nil{
            aCoder.encode(actualMoney, forKey: "actualMoney")
        }
        if dailyInterest != nil{
            aCoder.encode(dailyInterest, forKey: "dailyInterest")
        }
        if debitFee != nil{
            aCoder.encode(debitFee, forKey: "debitFee")
        }
        if debitMoney != nil{
            aCoder.encode(debitMoney, forKey: "debitMoney")
        }
        if debitPeriod != nil{
            aCoder.encode(debitPeriod, forKey: "debitPeriod")
        }
        if descriptionField != nil{
            aCoder.encode(descriptionField, forKey: "description")
        }
        if overdueDayInterest != nil{
            aCoder.encode(overdueDayInterest, forKey: "overdueDayInterest")
        }
        if payBackMoney != nil{
            aCoder.encode(payBackMoney, forKey: "payBackMoney")
        }
        
    }
    
    class func getDebitStyle() -> Promise<DebitStyle> {
        var debitStyle : DebitStyle!
        let dataFn = { (json: JSON) -> Void in
            let obj = json.array![0]["debitCombination"].array![0]
            debitStyle = DebitStyle(fromJson: obj)
        }
        let request = RequestData(path: "Main/GetInitDebitStyleV2")
        return netRequest(request, dataFn: dataFn).then { _ in
            return Promise(value: debitStyle)
        }
    }
    
    
}
